/*
 * Contacts.java
 *
 * Created on May 23, 2008, 6:35 PM
 */

package org.potomak.patientmanager.gui;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Set;
import java.util.Date;
import java.lang.Double;

import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.TableModelEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.ListModel;
import javax.swing.table.TableModel;
import javax.swing.ComboBoxModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.DefaultComboBoxModel;
import javax.swing.AbstractListModel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JWindow;
import javax.swing.JFrame;
import javax.swing.JDialog;

import javax.jdo.Transaction;
import javax.jdo.JDOHelper;

import javax.swing.DefaultListSelectionModel;
import javax.swing.JTable;
import javax.swing.event.TableModelListener;
import org.potomak.patientmanager.beans.model.PatientBean;
import org.potomak.patientmanager.beans.model.CheckUpBean;
import org.potomak.patientmanager.beans.model.CheckUpBean;
import org.potomak.patientmanager.beans.model.SuggestionNoteBean;
import org.potomak.patientmanager.beans.model.ImportantNoteBean;
import org.potomak.patientmanager.beans.model.SimpleNoteBean;
import org.potomak.patientmanager.persistence.JPoxJDO;

/**
 *
 * @author  mobile
 */
public class Contacts extends javax.swing.JFrame {

    private JPoxJDO jpoxJDO;
    private PatientBean[] patients;
    private int selectedPatientIndex;
    private int selectedCheckUpIndex;
    private ListModel patientListModel;
    private TableModel checkUpsTableModel;
    private ComboBoxModel genderListModel;
    private boolean patientInfoEditable = false;
    private boolean notesEditable = false;
    private boolean importantNotesEditable = false;
    private boolean suggestionsEditable = false;
    private SimpleDateFormat contactsDateFormat = new SimpleDateFormat("dd/MM/yyyy");
    
    class PatientsListModel extends DefaultComboBoxModel {
        public PatientsListModel(Object[] data) {
            super(data);
        }
    }
    
    class CheckUpsTableModel extends DefaultTableModel {
        public CheckUpsTableModel() {
            super(new Object[][] {},new String[] {"Date","Weight"});
        }
        
        public boolean isCellEditable(int row, int column) {
            return false;
        }
        
        public void setDataVector(Set checkUps) {
            Object[] checkUpsArray = checkUps.toArray();
            Object[][] checkUpsDataVector = new Object[checkUpsArray.length][];
            for(int i = 0; i < checkUpsArray.length; i++) {
                checkUpsDataVector[i] = new Object[2];
                checkUpsDataVector[i][0] = contactsDateFormat.format(((CheckUpBean)checkUpsArray[i]).getDate());
                checkUpsDataVector[i][1] = ((CheckUpBean)checkUpsArray[i]).getWeight();
            }
            
            super.setDataVector(checkUpsDataVector, new String[] {"Date","Weight"});
        }
    }
    
    /** Creates new form Contacts */
    public Contacts(JPoxJDO jpoxJDO,PatientBean[] patients) {
        
        this.patients = patients;
        this.jpoxJDO = jpoxJDO;
        
        patientListModel = new PatientsListModel(new Object [] {});
        
        genderListModel = new GenderComboBoxModel();
        
        checkUpsTableModel = new CheckUpsTableModel();
        
        initComponents();
        
        setPatientListData();
        
        checkUpsTable.getSelectionModel().addListSelectionListener(
            new ListSelectionListener() {
                public void valueChanged(ListSelectionEvent event) {
                    checkUpsListValueChanged(event);
                }
            }
        );
    }
    
    public JPoxJDO getJpoxJDO() {
        return this.jpoxJDO;
    }
    
    public PatientBean[] getPatients() {
        return this.patients;
    }
    
    public PatientBean getSelectedPatient() {
        return this.patients[selectedPatientIndex];
    }
    
    private void setPatientListData() {
        ListItem[] rows = new ListItem[patients.length];
        for(int i = 0; i < patients.length; i++) {
            rows[i] = new ListItem(patients[i].getId(),patients[i].getName());
        }
        
        patientList.setModel(new PatientsListModel(rows));
    }
    
    public void updatePatientListData() {
        patients = jpoxJDO.patientList();
        setPatientListData();
    }
    
    private void setCheckUpsTableData() {
        ((CheckUpsTableModel)checkUpsTable.getModel()).setDataVector(patients[selectedPatientIndex].getCheckUps());
    }
    
    public void updateCheckUpsTableData() {
        setCheckUpsTableData();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabsPanel = new javax.swing.JTabbedPane();
        importantNotesTab = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        importantNotesTextArea = new javax.swing.JTextArea();
        cancelImportantNotesButton = new javax.swing.JButton();
        modifySaveImportantNotesButton = new javax.swing.JButton();
        notesTab = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        notesTextArea = new javax.swing.JTextArea();
        cancelNotesButton = new javax.swing.JButton();
        modifySaveNotesButton = new javax.swing.JButton();
        checkUpsTab = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        checkUpsTable = new javax.swing.JTable();
        showChartButton = new javax.swing.JButton();
        removeCheckUpButton = new javax.swing.JButton();
        addCheckUpButton = new javax.swing.JButton();
        patientInfoTab = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        heightLabel = new javax.swing.JLabel();
        weightLabel = new javax.swing.JLabel();
        birthDayLabel = new javax.swing.JLabel();
        genderLabel = new javax.swing.JLabel();
        nameLabel = new javax.swing.JLabel();
        genderField = new javax.swing.JComboBox();
        nameField = new javax.swing.JTextField();
        birthDayField = new javax.swing.JTextField();
        weightField = new javax.swing.JTextField();
        heightField = new javax.swing.JTextField();
        dateLabel = new javax.swing.JLabel();
        dateField = new javax.swing.JLabel();
        modifySavePatientButton = new javax.swing.JButton();
        cancelPatientButton = new javax.swing.JButton();
        suggestionsTab = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        suggestionsTextArea = new javax.swing.JTextArea();
        cancelSuggestionsButton = new javax.swing.JButton();
        modifySaveSuggestionsButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        patientList = new javax.swing.JList();
        addPatientButton = new javax.swing.JButton();
        removePatientButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Contacts");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        tabsPanel.setRequestFocusEnabled(false);

        importantNotesTextArea.setColumns(20);
        importantNotesTextArea.setRows(5);
        importantNotesTextArea.setEnabled(false);
        jScrollPane4.setViewportView(importantNotesTextArea);

        cancelImportantNotesButton.setText("Cancel");
        cancelImportantNotesButton.setEnabled(false);
        cancelImportantNotesButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelImportantNotesButtonActionPerformed(evt);
            }
        });

        modifySaveImportantNotesButton.setText("Modify");
        modifySaveImportantNotesButton.setEnabled(false);
        modifySaveImportantNotesButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modifySaveImportantNotesButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout importantNotesTabLayout = new org.jdesktop.layout.GroupLayout(importantNotesTab);
        importantNotesTab.setLayout(importantNotesTabLayout);
        importantNotesTabLayout.setHorizontalGroup(
            importantNotesTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, importantNotesTabLayout.createSequentialGroup()
                .addContainerGap()
                .add(importantNotesTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(importantNotesTabLayout.createSequentialGroup()
                        .add(cancelImportantNotesButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(modifySaveImportantNotesButton))
                    .add(jScrollPane4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 587, Short.MAX_VALUE))
                .addContainerGap())
        );
        importantNotesTabLayout.setVerticalGroup(
            importantNotesTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(importantNotesTabLayout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 313, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 19, Short.MAX_VALUE)
                .add(importantNotesTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(modifySaveImportantNotesButton)
                    .add(cancelImportantNotesButton))
                .addContainerGap())
        );

        tabsPanel.addTab("Important notes", importantNotesTab);

        notesTextArea.setColumns(20);
        notesTextArea.setRows(5);
        notesTextArea.setEnabled(false);
        jScrollPane3.setViewportView(notesTextArea);

        cancelNotesButton.setText("Cancel");
        cancelNotesButton.setEnabled(false);
        cancelNotesButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelNotesButtonActionPerformed(evt);
            }
        });

        modifySaveNotesButton.setText("Modify");
        modifySaveNotesButton.setEnabled(false);
        modifySaveNotesButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modifySaveNotesButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout notesTabLayout = new org.jdesktop.layout.GroupLayout(notesTab);
        notesTab.setLayout(notesTabLayout);
        notesTabLayout.setHorizontalGroup(
            notesTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, notesTabLayout.createSequentialGroup()
                .addContainerGap()
                .add(notesTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(notesTabLayout.createSequentialGroup()
                        .add(cancelNotesButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(modifySaveNotesButton))
                    .add(jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 587, Short.MAX_VALUE))
                .addContainerGap())
        );
        notesTabLayout.setVerticalGroup(
            notesTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(notesTabLayout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 313, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 19, Short.MAX_VALUE)
                .add(notesTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(modifySaveNotesButton)
                    .add(cancelNotesButton))
                .addContainerGap())
        );

        tabsPanel.addTab("Notes", notesTab);

        checkUpsTable.setModel(checkUpsTableModel);
        checkUpsTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(checkUpsTable);

        showChartButton.setText("Show chart");
        showChartButton.setEnabled(false);
        showChartButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showChartButtonActionPerformed(evt);
            }
        });

        removeCheckUpButton.setText("Remove");
        removeCheckUpButton.setEnabled(false);
        removeCheckUpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeCheckUpButtonActionPerformed(evt);
            }
        });

        addCheckUpButton.setText("Add");
        addCheckUpButton.setEnabled(false);
        addCheckUpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addCheckUpButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout checkUpsTabLayout = new org.jdesktop.layout.GroupLayout(checkUpsTab);
        checkUpsTab.setLayout(checkUpsTabLayout);
        checkUpsTabLayout.setHorizontalGroup(
            checkUpsTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, checkUpsTabLayout.createSequentialGroup()
                .addContainerGap()
                .add(checkUpsTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(checkUpsTabLayout.createSequentialGroup()
                        .add(addCheckUpButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(removeCheckUpButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(showChartButton))
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 587, Short.MAX_VALUE))
                .addContainerGap())
        );
        checkUpsTabLayout.setVerticalGroup(
            checkUpsTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(checkUpsTabLayout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 313, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 19, Short.MAX_VALUE)
                .add(checkUpsTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(showChartButton)
                    .add(removeCheckUpButton)
                    .add(addCheckUpButton))
                .addContainerGap())
        );

        tabsPanel.addTab("Check ups", checkUpsTab);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Contact info"));

        heightLabel.setText("Height");

        weightLabel.setText("Initial weight");

        birthDayLabel.setText("Date of birth");

        genderLabel.setText("Gender");

        nameLabel.setText("Name");

        genderField.setModel(genderListModel);
        genderField.setEnabled(false);

        nameField.setEnabled(false);

        birthDayField.setEnabled(false);

        weightField.setEnabled(false);

        heightField.setEnabled(false);

        dateLabel.setText("Added");

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(genderLabel)
                            .add(nameLabel)
                            .add(birthDayLabel)
                            .add(weightLabel)
                            .add(heightLabel))
                        .add(69, 69, 69)
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(nameField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
                            .add(genderField, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(birthDayField)
                            .add(weightField)
                            .add(heightField)))
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(dateLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(dateField)))
                .addContainerGap(162, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(nameLabel)
                    .add(nameField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(genderLabel)
                    .add(genderField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(birthDayLabel)
                    .add(birthDayField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(weightLabel)
                    .add(weightField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(heightLabel)
                    .add(heightField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(dateLabel)
                    .add(dateField))
                .addContainerGap(96, Short.MAX_VALUE))
        );

        modifySavePatientButton.setText("Modify");
        modifySavePatientButton.setEnabled(false);
        modifySavePatientButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modifySavePatientButtonActionPerformed(evt);
            }
        });

        cancelPatientButton.setText("Cancel");
        cancelPatientButton.setEnabled(false);
        cancelPatientButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelPatientButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout patientInfoTabLayout = new org.jdesktop.layout.GroupLayout(patientInfoTab);
        patientInfoTab.setLayout(patientInfoTabLayout);
        patientInfoTabLayout.setHorizontalGroup(
            patientInfoTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, patientInfoTabLayout.createSequentialGroup()
                .addContainerGap()
                .add(patientInfoTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(patientInfoTabLayout.createSequentialGroup()
                        .add(cancelPatientButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(modifySavePatientButton))
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        patientInfoTabLayout.setVerticalGroup(
            patientInfoTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(patientInfoTabLayout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 9, Short.MAX_VALUE)
                .add(patientInfoTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(modifySavePatientButton)
                    .add(cancelPatientButton))
                .addContainerGap())
        );

        tabsPanel.addTab("Info", patientInfoTab);

        suggestionsTextArea.setColumns(20);
        suggestionsTextArea.setRows(5);
        suggestionsTextArea.setEnabled(false);
        jScrollPane5.setViewportView(suggestionsTextArea);

        cancelSuggestionsButton.setText("Cancel");
        cancelSuggestionsButton.setEnabled(false);
        cancelSuggestionsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelSuggestionsButtonActionPerformed(evt);
            }
        });

        modifySaveSuggestionsButton.setText("Modify");
        modifySaveSuggestionsButton.setEnabled(false);
        modifySaveSuggestionsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modifySaveSuggestionsButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout suggestionsTabLayout = new org.jdesktop.layout.GroupLayout(suggestionsTab);
        suggestionsTab.setLayout(suggestionsTabLayout);
        suggestionsTabLayout.setHorizontalGroup(
            suggestionsTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, suggestionsTabLayout.createSequentialGroup()
                .addContainerGap()
                .add(suggestionsTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(suggestionsTabLayout.createSequentialGroup()
                        .add(cancelSuggestionsButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(modifySaveSuggestionsButton))
                    .add(jScrollPane5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 587, Short.MAX_VALUE))
                .addContainerGap())
        );
        suggestionsTabLayout.setVerticalGroup(
            suggestionsTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(suggestionsTabLayout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 313, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 19, Short.MAX_VALUE)
                .add(suggestionsTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(modifySaveSuggestionsButton)
                    .add(cancelSuggestionsButton))
                .addContainerGap())
        );

        tabsPanel.addTab("Suggestions", suggestionsTab);

        patientList.setModel(patientListModel);
        patientList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        patientList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                patientListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(patientList);

        addPatientButton.setText("Add");
        addPatientButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addPatientButtonActionPerformed(evt);
            }
        });

        removePatientButton.setText("Remove");
        removePatientButton.setEnabled(false);
        removePatientButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removePatientButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(addPatientButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(removePatientButton))
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 247, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(tabsPanel)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 397, Short.MAX_VALUE)
                        .add(18, 18, 18)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(removePatientButton)
                            .add(addPatientButton)))
                    .add(tabsPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 444, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void setPatientInfoEditable(boolean isEditable) {
        if(isEditable) {
            patientList.setEnabled(false);
            addPatientButton.setEnabled(false);
            removePatientButton.setEnabled(false);
            tabsPanel.setEnabledAt(0,false);
            tabsPanel.setEnabledAt(1,false);
            tabsPanel.setEnabledAt(2,false);
            tabsPanel.setEnabledAt(4,false);
            cancelPatientButton.setEnabled(true);
            nameField.setEnabled(true);
            genderField.setEnabled(true);
            birthDayField.setEnabled(true);
            weightField.setEnabled(true);
            heightField.setEnabled(true);
            modifySavePatientButton.setText("Save");
        }
        else {
            patientList.setEnabled(true);
            addPatientButton.setEnabled(true);
            removePatientButton.setEnabled(true);
            tabsPanel.setEnabledAt(0,true);
            tabsPanel.setEnabledAt(1,true);
            tabsPanel.setEnabledAt(2,true);
            tabsPanel.setEnabledAt(4,true);
            cancelPatientButton.setEnabled(false);
            nameField.setEnabled(false);
            genderField.setEnabled(false);
            birthDayField.setEnabled(false);
            weightField.setEnabled(false);
            heightField.setEnabled(false);
            modifySavePatientButton.setText("Modify");
        }
    }
    
    private void setNotesEditable(boolean isEditable) {
        if(isEditable) {
            patientList.setEnabled(false);
            addPatientButton.setEnabled(false);
            removePatientButton.setEnabled(false);
            tabsPanel.setEnabledAt(0,false);
            tabsPanel.setEnabledAt(2,false);
            tabsPanel.setEnabledAt(3,false);
            tabsPanel.setEnabledAt(4,false);
            cancelNotesButton.setEnabled(true);
            notesTextArea.setEnabled(true);
            modifySaveNotesButton.setText("Save");
        }
        else {
            patientList.setEnabled(true);
            addPatientButton.setEnabled(true);
            removePatientButton.setEnabled(true);
            tabsPanel.setEnabledAt(0,true);
            tabsPanel.setEnabledAt(2,true);
            tabsPanel.setEnabledAt(3,true);
            tabsPanel.setEnabledAt(4,true);
            cancelNotesButton.setEnabled(false);
            notesTextArea.setEnabled(false);
            modifySaveNotesButton.setText("Modify");
        }
    }
    
    private void setImportantNotesEditable(boolean isEditable) {
        if(isEditable) {
            patientList.setEnabled(false);
            addPatientButton.setEnabled(false);
            removePatientButton.setEnabled(false);
            tabsPanel.setEnabledAt(1,false);
            tabsPanel.setEnabledAt(2,false);
            tabsPanel.setEnabledAt(3,false);
            tabsPanel.setEnabledAt(4,false);
            cancelImportantNotesButton.setEnabled(true);
            importantNotesTextArea.setEnabled(true);
            modifySaveImportantNotesButton.setText("Save");
        }
        else {
            patientList.setEnabled(true);
            addPatientButton.setEnabled(true);
            removePatientButton.setEnabled(true);
            tabsPanel.setEnabledAt(1,true);
            tabsPanel.setEnabledAt(2,true);
            tabsPanel.setEnabledAt(3,true);
            tabsPanel.setEnabledAt(4,true);
            cancelImportantNotesButton.setEnabled(false);
            importantNotesTextArea.setEnabled(false);
            modifySaveImportantNotesButton.setText("Modify");
        }
    }
    
    private void setSuggestionsEditable(boolean isEditable) {
        if(isEditable) {
            patientList.setEnabled(false);
            addPatientButton.setEnabled(false);
            removePatientButton.setEnabled(false);
            tabsPanel.setEnabledAt(0,false);
            tabsPanel.setEnabledAt(1,false);
            tabsPanel.setEnabledAt(2,false);
            tabsPanel.setEnabledAt(3,false);
            cancelSuggestionsButton.setEnabled(true);
            suggestionsTextArea.setEnabled(true);
            modifySaveSuggestionsButton.setText("Save");
        }
        else {
            patientList.setEnabled(true);
            addPatientButton.setEnabled(true);
            removePatientButton.setEnabled(true);
            tabsPanel.setEnabledAt(0,true);
            tabsPanel.setEnabledAt(1,true);
            tabsPanel.setEnabledAt(2,true);
            tabsPanel.setEnabledAt(3,true);
            cancelSuggestionsButton.setEnabled(false);
            suggestionsTextArea.setEnabled(false);
            modifySaveSuggestionsButton.setText("Modify");
        }
    }
    
    
private void modifySavePatientButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modifySavePatientButtonActionPerformed
    if(!patientInfoEditable) {
        patientInfoEditable = true;
        setPatientInfoEditable(true);
    }
    else {
        //default icon, custom title
        int wantSave = JOptionPane.showConfirmDialog(this,"Do you want to save changes made?","Save",JOptionPane.YES_NO_OPTION);
        System.out.println("Confirm save contact info dialog result: "+wantSave);
        
        if (wantSave == 0) {
            boolean success = false;
            
            Transaction txn = jpoxJDO.getPersistenceManager().currentTransaction();
            try {
                txn.begin();

                Object id = jpoxJDO.getPersistenceManager().getObjectId(patients[selectedPatientIndex]);
                System.out.println("searching element id "+id);
                PatientBean patientBean = (PatientBean)jpoxJDO.getPersistenceManager().getObjectById(id);
                patientBean.setName(nameField.getText().trim());
                try {
                    patientBean.setBirthDay(contactsDateFormat.parse(birthDayField.getText()));
                }
                catch(Exception e) {
                    e.printStackTrace();
                }
                patientBean.setGender(((ListItem) genderField.getModel().getSelectedItem()).getKey().toString().trim());
                patientBean.setHeight(new Double(heightField.getText()));
                patientBean.setWeight(new Double(weightField.getText()));
            
                txn.commit();
                success = true;
                updatePatientListData();
            } finally {
                if (txn.isActive()) {
                    txn.rollback();
                    success = false;
                }
            }

            if (!success) {
                JOptionPane.showMessageDialog(this, "Contact info not saved.","Error",JOptionPane.ERROR_MESSAGE);
            }

            patientInfoEditable = false;
            setPatientInfoEditable(false);
        }
    }
}//GEN-LAST:event_modifySavePatientButtonActionPerformed

private void cancelPatientButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelPatientButtonActionPerformed
    patientInfoEditable = false;
    setPatientInfoEditable(false);
}//GEN-LAST:event_cancelPatientButtonActionPerformed

private void patientListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_patientListValueChanged
    if(evt.getValueIsAdjusting()) {
        ListItem selectedItem = (ListItem)((javax.swing.JList)evt.getSource()).getSelectedValue();
        if(selectedItem != null) {
            if(!modifySavePatientButton.isEnabled()) {
                modifySavePatientButton.setEnabled(true);
            }
            if(!modifySaveImportantNotesButton.isEnabled()) {
                modifySaveImportantNotesButton.setEnabled(true);
            }
            if(!modifySaveNotesButton.isEnabled()) {
                modifySaveNotesButton.setEnabled(true);
            }
            if(!modifySaveSuggestionsButton.isEnabled()) {
                modifySaveSuggestionsButton.setEnabled(true);
            }
            if(!showChartButton.isEnabled()) {
                showChartButton.setEnabled(true);
            }
            if(!addCheckUpButton.isEnabled()) {
                addCheckUpButton.setEnabled(true);
            }
            removePatientButton.setEnabled(true);
            int i;
            for(i = 0; i < patients.length && patients[i].getId() != (Integer)(selectedItem.getKey()); i++);
            selectedPatientIndex = i;
            nameField.setText(patients[i].getName());
            ListItem genderItem;
            if(patients[i].getGender().equals("M")) {
                genderItem = new ListItem("M","Male");
            }
            else {
                genderItem = new ListItem("F","Female");
            }
            genderField.getModel().setSelectedItem(genderItem);
            birthDayField.setText(contactsDateFormat.format(patients[i].getBirthDay()));
            weightField.setText(patients[i].getWeight().toString());
            heightField.setText(patients[i].getHeight().toString());
            dateField.setText(contactsDateFormat.format(patients[i].getDate()));

            ((CheckUpsTableModel)checkUpsTableModel).setDataVector(patients[i].getCheckUps());
            
            Object[] suggestions = patients[i].getSuggestionNotes().toArray();
            if(suggestions.length > 0) {
                suggestionsTextArea.setText(((SuggestionNoteBean)suggestions[0]).getText());
            }
            
            Object[] importantNotes = patients[i].getImportantNotes().toArray();
            if(importantNotes.length > 0) {
                importantNotesTextArea.setText(((ImportantNoteBean)importantNotes[0]).getText());
            }
            
            Object[] simpleNotes = patients[i].getSimpleNotes().toArray();
            if(simpleNotes.length > 0) {
                notesTextArea.setText(((SimpleNoteBean)simpleNotes[0]).getText());
            }
        }
        else {
            addPatientButton.setEnabled(false);
            removePatientButton.setEnabled(false);
        }
    }
}//GEN-LAST:event_patientListValueChanged

private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
    System.out.println("Shutting down JPOX persistence manager.");
    jpoxJDO.shutdown();
    System.out.println("***********************************************************");
    System.exit(0);
}//GEN-LAST:event_formWindowClosing

private void cancelSuggestionsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelSuggestionsButtonActionPerformed
    suggestionsEditable = false;
    setSuggestionsEditable(false);
}//GEN-LAST:event_cancelSuggestionsButtonActionPerformed

private void modifySaveSuggestionsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modifySaveSuggestionsButtonActionPerformed
    if(!suggestionsEditable) {
        suggestionsEditable = true;
        setSuggestionsEditable(true);
    }
    else {
        int wantSave = JOptionPane.showConfirmDialog(this,"Do you want to save changes made?","Save",JOptionPane.YES_NO_OPTION);
        System.out.println("Confirm save suggestions dialog result: "+wantSave);
        
        if (wantSave == 0) {
            boolean success = false;
            
            Transaction txn = jpoxJDO.getPersistenceManager().currentTransaction();
            try {
                txn.begin();
                
                Object[] suggestions = patients[selectedPatientIndex].getSuggestionNotes().toArray();
                SuggestionNoteBean suggestion = null;
                if(suggestions.length > 0) {
                    suggestion = (SuggestionNoteBean)suggestions[0];
                }
                Object id = jpoxJDO.getPersistenceManager().getObjectId(suggestion);
                System.out.println("searching element id "+id);
                SuggestionNoteBean suggestionNoteBean = (SuggestionNoteBean)jpoxJDO.getPersistenceManager().getObjectById(id);
                suggestionNoteBean.setText(suggestionsTextArea.getText().trim());
            
                txn.commit();
                success = true;
            } finally {
                if (txn.isActive()) {
                    txn.rollback();
                    success = false;
                }
            }

            if (!success) {
                JOptionPane.showMessageDialog(this, "Suggestions not saved.","Error",JOptionPane.ERROR_MESSAGE);
            }

            suggestionsEditable = false;
            setSuggestionsEditable(false);
        }
    }
}//GEN-LAST:event_modifySaveSuggestionsButtonActionPerformed

private void cancelNotesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelNotesButtonActionPerformed
    notesEditable = false;
    setNotesEditable(false);
}//GEN-LAST:event_cancelNotesButtonActionPerformed

private void modifySaveNotesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modifySaveNotesButtonActionPerformed
    if(!notesEditable) {
        notesEditable = true;
        setNotesEditable(true);
    }
    else {
        int wantSave = JOptionPane.showConfirmDialog(this,"Do you want to save changes made?","Save",JOptionPane.YES_NO_OPTION);
        System.out.println("Confirm save notes dialog result: "+wantSave);
        
        if (wantSave == 0) {
            boolean success = false;
            
            Transaction txn = jpoxJDO.getPersistenceManager().currentTransaction();
            try {
                txn.begin();
                
                Object[] notes = patients[selectedPatientIndex].getSimpleNotes().toArray();
                SimpleNoteBean note = null;
                if(notes.length > 0) {
                    note = (SimpleNoteBean)notes[0];
                }
                Object id = jpoxJDO.getPersistenceManager().getObjectId(note);
                System.out.println("searching element id "+id);
                SimpleNoteBean simpleNoteBean = (SimpleNoteBean)jpoxJDO.getPersistenceManager().getObjectById(id);
                simpleNoteBean.setText(notesTextArea.getText().trim());
            
                txn.commit();
                success = true;
            } finally {
                if (txn.isActive()) {
                    txn.rollback();
                    success = false;
                }
            }

            if (!success) {
                JOptionPane.showMessageDialog(this, "Notes not saved.","Error",JOptionPane.ERROR_MESSAGE);
            }

            notesEditable = false;
            setNotesEditable(false);
        }
    }
}//GEN-LAST:event_modifySaveNotesButtonActionPerformed

private void cancelImportantNotesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelImportantNotesButtonActionPerformed
    importantNotesEditable = false;
    setImportantNotesEditable(false);
}//GEN-LAST:event_cancelImportantNotesButtonActionPerformed

private void modifySaveImportantNotesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modifySaveImportantNotesButtonActionPerformed
    if(!importantNotesEditable) {
        importantNotesEditable = true;
        setImportantNotesEditable(true);
    }
    else {
        int wantSave = JOptionPane.showConfirmDialog(this,"Do you want to save changes made?","Save",JOptionPane.YES_NO_OPTION);
        System.out.println("Confirm save important notes dialog result: "+wantSave);
        
        if (wantSave == 0) {
            boolean success = false;
            
            Transaction txn = jpoxJDO.getPersistenceManager().currentTransaction();
            try {
                txn.begin();
                
                Object[] impNotes = patients[selectedPatientIndex].getImportantNotes().toArray();
                ImportantNoteBean impNote = null;
                if(impNotes.length > 0) {
                    impNote = (ImportantNoteBean)impNotes[0];
                }
                Object id = jpoxJDO.getPersistenceManager().getObjectId(impNote);
                System.out.println("searching element id "+id);
                ImportantNoteBean importantNoteBean = (ImportantNoteBean)jpoxJDO.getPersistenceManager().getObjectById(id);
                importantNoteBean.setText(importantNotesTextArea.getText().trim());
            
                txn.commit();
                success = true;
            } finally {
                if (txn.isActive()) {
                    txn.rollback();
                    success = false;
                }
            }

            if (!success) {
                JOptionPane.showMessageDialog(this, "Important notes not saved.","Error",JOptionPane.ERROR_MESSAGE);
            }

            importantNotesEditable = false;
            setImportantNotesEditable(false);
        }
    }
}//GEN-LAST:event_modifySaveImportantNotesButtonActionPerformed

private void showChartButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showChartButtonActionPerformed
    Object[] checkUps = patients[selectedPatientIndex].getCheckUps().toArray();
    
    class CheckUpsComparator implements Comparator {
            public int compare(Object obj0, Object obj1) {
                if(((CheckUpBean)obj0).getDate().getTime() < ((CheckUpBean)obj1).getDate().getTime()) {
                    return -1;
                }
                else if(((CheckUpBean)obj0).getDate().getTime() > ((CheckUpBean)obj1).getDate().getTime()) {
                    return 1;
                }
                else {
                    return 0;
                }
            }
    }
    
    Arrays.sort(checkUps,new CheckUpsComparator());
    
    CheckUpBean[] castedArray = new CheckUpBean[checkUps.length];
    for(int i = 0; i < checkUps.length; i++) {
        castedArray[i] = (CheckUpBean)checkUps[i];
    }
    
    JDialog chartWindow = new ChartDialog(this,castedArray);
    chartWindow.setSize(640, 480);
    chartWindow.validate();
    chartWindow.setVisible(true);
    ((ChartDialog)chartWindow).refresh();
}//GEN-LAST:event_showChartButtonActionPerformed

private void addPatientButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addPatientButtonActionPerformed
    JDialog addPatientWindow = new NewPatientDialog(this);
    addPatientWindow.setVisible(true);
}//GEN-LAST:event_addPatientButtonActionPerformed

private void removePatientButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removePatientButtonActionPerformed

    String patientName = patients[selectedPatientIndex].getName();
    
    int wantRemove = JOptionPane.showConfirmDialog(this,"Do you want to remove contact "+patientName+"?","Remove",JOptionPane.YES_NO_OPTION);
    System.out.println("Confirm remove patient dialog result: "+wantRemove);

    if (wantRemove == 0) {
        boolean success = false;

        Transaction txn = jpoxJDO.getPersistenceManager().currentTransaction();
        try {
            txn.begin();

            Object id = jpoxJDO.getPersistenceManager().getObjectId(patients[selectedPatientIndex]);
            System.out.println("searching element id "+id);
            PatientBean patientBean = (PatientBean)jpoxJDO.getPersistenceManager().getObjectById(id);
            jpoxJDO.getPersistenceManager().deletePersistent(patientBean);

            txn.commit();
            success = true;
        } finally {
            if (txn.isActive()) {
                txn.rollback();
                success = false;
            }
        }

        if (success) {
            modifySavePatientButton.setEnabled(false);
            modifySaveImportantNotesButton.setEnabled(false);
            modifySaveNotesButton.setEnabled(false);
            modifySaveSuggestionsButton.setEnabled(false);
            showChartButton.setEnabled(false);
            addCheckUpButton.setEnabled(false);
            removePatientButton.setEnabled(false);
        } else {
            JOptionPane.showMessageDialog(this, "Error removing contact.","Error",JOptionPane.ERROR_MESSAGE);
        }
        
        updatePatientListData();
    }
}//GEN-LAST:event_removePatientButtonActionPerformed

private void addCheckUpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addCheckUpButtonActionPerformed
    JDialog addCheckUpWindow = new NewCheckUpDialog(this);
    addCheckUpWindow.setVisible(true);
}//GEN-LAST:event_addCheckUpButtonActionPerformed

private void checkUpsListValueChanged(javax.swing.event.ListSelectionEvent evt) {
    if(evt.getValueIsAdjusting()) {
        selectedCheckUpIndex = ((DefaultListSelectionModel)evt.getSource()).getLeadSelectionIndex();
        if(selectedCheckUpIndex != -1) {
            removeCheckUpButton.setEnabled(true);
        }
        else {
            removeCheckUpButton.setEnabled(false);
        }
        System.out.println("selected row: "+selectedCheckUpIndex);
    }
}


private void removeCheckUpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeCheckUpButtonActionPerformed
    int wantRemove = JOptionPane.showConfirmDialog(this,"Do you want to remove check up?","Remove",JOptionPane.YES_NO_OPTION);

    if (wantRemove == 0) {
        boolean success = false;

        Transaction txn = jpoxJDO.getPersistenceManager().currentTransaction();
        try {
            txn.begin();
            
            CheckUpBean checkUpBean = new CheckUpBean();
            try {
                checkUpBean.setDate(contactsDateFormat.parse((String) checkUpsTable.getModel().getValueAt(selectedCheckUpIndex, 0)));
            } catch (ParseException ex) {
                Logger.getLogger(Contacts.class.getName()).log(Level.SEVERE, null, ex);
            }
            checkUpBean.setWeight((Double)checkUpsTable.getModel().getValueAt(selectedCheckUpIndex, 1));
            checkUpBean.setPatient(getSelectedPatient());
            
            //Object id = jpoxJDO.getPersistenceManager().getObjectId(checkUpBean);
            //System.out.println("searching element id "+id);
            //checkUpBean = (CheckUpBean)jpoxJDO.getPersistenceManager().getObjectById(id);
            //jpoxJDO.getPersistenceManager().deletePersistent(checkUpBean);
            
            System.out.println("Does patient own this check up? "+getSelectedPatient().getCheckUps().contains(checkUpBean));
            Iterator i = getSelectedPatient().getCheckUps().iterator();
            
            int num = 0;
            while(i.hasNext()) {
                num++;
                CheckUpBean cub = (CheckUpBean)i.next();
                
                System.out.println("confronto #"+num+" =>\n"+cub+"\n == \n"+checkUpBean+" ?? ==>"+cub.equals(checkUpBean)+"\n\n");
                
                if(cub.equals(checkUpBean)) {
                    jpoxJDO.getPersistenceManager().deletePersistent(cub);
                }
            }
            
            txn.commit();
            success = true;
        } finally {
            if (txn.isActive()) {
                txn.rollback();
                success = false;
            }
        }

        if (success) {
            removeCheckUpButton.setEnabled(false);
        } else {
            JOptionPane.showMessageDialog(this, "Error removing check up.","Error",JOptionPane.ERROR_MESSAGE);
        }
        
        updateCheckUpsTableData();
    }
}//GEN-LAST:event_removeCheckUpButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addCheckUpButton;
    private javax.swing.JButton addPatientButton;
    private javax.swing.JTextField birthDayField;
    private javax.swing.JLabel birthDayLabel;
    private javax.swing.JButton cancelImportantNotesButton;
    private javax.swing.JButton cancelNotesButton;
    private javax.swing.JButton cancelPatientButton;
    private javax.swing.JButton cancelSuggestionsButton;
    private javax.swing.JPanel checkUpsTab;
    private javax.swing.JTable checkUpsTable;
    private javax.swing.JLabel dateField;
    private javax.swing.JLabel dateLabel;
    private javax.swing.JComboBox genderField;
    private javax.swing.JLabel genderLabel;
    private javax.swing.JTextField heightField;
    private javax.swing.JLabel heightLabel;
    private javax.swing.JPanel importantNotesTab;
    private javax.swing.JTextArea importantNotesTextArea;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JButton modifySaveImportantNotesButton;
    private javax.swing.JButton modifySaveNotesButton;
    private javax.swing.JButton modifySavePatientButton;
    private javax.swing.JButton modifySaveSuggestionsButton;
    private javax.swing.JTextField nameField;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JPanel notesTab;
    private javax.swing.JTextArea notesTextArea;
    private javax.swing.JPanel patientInfoTab;
    private javax.swing.JList patientList;
    private javax.swing.JButton removeCheckUpButton;
    private javax.swing.JButton removePatientButton;
    private javax.swing.JButton showChartButton;
    private javax.swing.JPanel suggestionsTab;
    private javax.swing.JTextArea suggestionsTextArea;
    private javax.swing.JTabbedPane tabsPanel;
    private javax.swing.JTextField weightField;
    private javax.swing.JLabel weightLabel;
    // End of variables declaration//GEN-END:variables

}
